opendir(DH,"/Users/yaping/Documents/workspace/data/dbSNP/VCF/") or die;
$total_count=0;
$total_convert_count=0;
foreach my $file(readdir(DH)){
	
	if($file=~/(CEU-\S+).vcf/){
		my $prefix=$1;
		open(FH,"</Users/yaping/Documents/workspace/data/dbSNP/VCF/$file") or die;
		my @in=<FH>;
		chomp(@in);
		close(FH);
		my @out;
		my @out_bed;
		my $count_total=0;
		my $count_convert=0;
		my %hash_snp=();
		foreach my $line(@in){
			if($line=~/^\#/){
				push(@out,"$line\n");
			}
			else{
				my @splitin = split "\t",$line;
				my $chr="chr".$splitin[0];
				my $start = $splitin[1];
				my $end = $splitin[1] + 1;
				push(@out_bed,"$chr\t$start\t$end\t$splitin[2]\n");
				$hash_snp{$splitin[2]}=$line;
				$count_total++;
				$total_count++;
				#print("$splitin[2]\n");
			}
			
		}
		my $out_bed_file = $prefix.".bed";
		my $out_vcf_file = $prefix.".hg18.vcf";
		my $mapped_bed_file = $prefix.".mapped.bed";
		my $unmapped_bed_file = $prefix.".unmapped.bed";
		open(OUT,">/Users/yaping/Documents/workspace/data/dbSNP/VCF/$out_bed_file");
		print OUT @out_bed;
		close(OUT);
		my $clause = "/Users/yaping/software/liftOver/liftOver /Users/yaping/Documents/workspace/data/dbSNP/VCF/$out_bed_file /Users/yaping/software/liftOver/hg19ToHg18.over.chain /Users/yaping/Documents/workspace/data/dbSNP/VCF/$mapped_bed_file /Users/yaping/Documents/workspace/data/dbSNP/VCF/$unmapped_bed_file";
		system(`$clause`);
		open(FH,"</Users/yaping/Documents/workspace/data/dbSNP/VCF/$mapped_bed_file") or die;
		my @in_new=<FH>;
		chomp(@in_new);
		close(FH);
		
		foreach my $line(@in_new){
			my @splitin = split "\t",$line;
			my $input_line=$hash_snp{$splitin[3]};
			if($input_line ne NULL){
				my @split_inline=split "\t",$input_line;
				push(@out,"$split_inline[0]\t$splitin[1]\t$split_inline[2]\t$split_inline[3]\t$split_inline[4]\t$split_inline[5]\t$split_inline[6]\t$split_inline[7]\n");
				$count_convert++;
				$total_convert_count++;
				#print("$split_inline[2]\n");
			}
		}
		print "$file count_total is : $count_total\ncount_convert is: $count_convert\n";
		open(OUT,">/Users/yaping/Documents/workspace/data/dbSNP/VCF/$out_vcf_file");
		print OUT @out;
		close(OUT);
	}
}
print "\nfinally, count_total is : $total_count\ncount_convert is: $total_convert_count\n";